/**
 * 视频操作
 *
 */
class videoOperate {
    constructor(myVideo) {
        this.videoContext = wx.createVideoContext(myVideo);
        this.timerIsOpen = false;
        this.init = this._init();
    }

    _init() {
        let _this = this;

        _this.showThis()
        return 'init success'
    }

    playVideoOnTime(t0, timelong, cb) { //播放指定时间间隔的视频
        let _this = this;

        _this.pause(); //播放视频前先暂停视频
        _this.seek(t0); //跳到t0的位置播放
        _this.play(); //开始播放

        _this.clearTimer();
        _this.createTimer(timelong, function() {
            _this.pause();
            typeof cb == 'function' && cb();
        })

        console.log('播放指定时间段的视频');
    }

    createTimer(timelong, cb) { //创建一个定时器
        let _this = this;

        var timelong = timelong;
        _this.timerIsOpen = true;

        _this.timer = setInterval(function() {
            timelong--;
            console.log('timelong', timelong);
            if (timelong <= 0) {
                _this.timerIsOpen = false;
                typeof cb == 'function' && cb();
            }
        }, 1000)
    }

    clearTimer() { //清除定时器
        let _this = this;

        clearInterval(_this.timer);
    }

    play() { //播放视频
        let _this = this;

        _this.videoContext.play();
        console.log('播放视频')
    }
    pause(cb) { //暂停视频
        let _this = this;

        _this.videoContext.pause();
        typeof cb == 'function' && cb();
        console.log('暂停视频')
    }
    seek(time) { //跳转到指定位置，单位 s
        let _this = this;

        _this.videoContext.seek(time);
        console.log('跳转到指定位置', )
    }
    sendDanmu(danmu) { //发送弹幕，danmu 包含两个属性 text, color
        let _this = this;

        _this.videoContext.sendDanmu(danmu);
        console.log('发送弹幕')
    }
    playbackRate(rate) { //设置倍速播放，支持的倍率有 0.5/0.8/1.0/1.25/1.5
        let _this = this;

        _this.videoContext.playbackRate(rate)
        console.log('设置播放速度', rate)
    }
    requestFullScreen() { //进入全屏，可传入{direction}参数
        let _this = this;

        _this.videoContext.requestFullScreen();
        console.log('进入全屏')
    }
    exitFullScreen() { //退出全屏
        let _this = this;

        _this.videoContext.exitFullScreen();
        console.log('退出全屏');
    }

    videoPlay() { //视频播放回调
        let _this = this;

        console.log('视频播放回调');
    }

    videoPause() { //视频暂停回调
        let _this = this;

        console.log('视频暂停回调');
    }
    videoEnded() { //视频结束回调
        let _this = this;

        console.log('视频结束回调');
    }
    videoTimeUpdate(e) { //视频播放进度回调
        let _this = this;

        console.log('视频播放进度回调', e.detail);
    }

    showThis() {
        console.log('showThis', this)
    }
}

module.exports = videoOperate;